/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package presentation.view;

import java.util.ArrayList;
import java.util.List;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import presentation.controller.ControlPanelMain;
import presentation.controller.ControlPanelMyAccount;
import vo.ProjectVO;

/**
 *
 * @author zergio
 */
public class PanelAddProject extends javax.swing.JPanel implements Subject{
    private List<Observer> observers;

    /**
     * Creates new form PanelAddProject
     */
    public PanelAddProject(AuxiliarFrame parent) {
        this.parent = parent;
        observers = new ArrayList<>();
        attach(MainPanel.getInstance());
        initComponents();
    }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    AuxiliarFrame parent;

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jTextFieldProjectName = new javax.swing.JTextField();
        jComboBoxProjectType = new javax.swing.JComboBox();
        jButtonSaveProject = new javax.swing.JButton();
        jButtonExitPanel = new javax.swing.JButton();

        jLabel1.setText("Nombre");

        jLabel2.setText("Tipo");

        jComboBoxProjectType.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Personal", "Trabajo", "Educación" }));

        jButtonSaveProject.setIcon(new javax.swing.ImageIcon(getClass().getResource("/presentation/view/images/Save.png"))); // NOI18N
        jButtonSaveProject.setText("Guardar");
        jButtonSaveProject.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonSaveProjectActionPerformed(evt);
            }
        });

        jButtonExitPanel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/presentation/view/images/Cancel.png"))); // NOI18N
        jButtonExitPanel.setText("Cancelar");
        jButtonExitPanel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonExitPanelActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jTextFieldProjectName)
                            .addComponent(jComboBoxProjectType, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButtonSaveProject)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButtonExitPanel)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jTextFieldProjectName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jComboBoxProjectType, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonSaveProject)
                    .addComponent(jButtonExitPanel))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonSaveProjectActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonSaveProjectActionPerformed

        ProjectVO vo = new ProjectVO();
        vo.setName(jTextFieldProjectName.getText());
        vo.setType(jComboBoxProjectType.getSelectedIndex());
        vo.setTasks(null);
        int id = new ControlPanelMyAccount().findByIp().getCalendarId();
        vo.setCalendarId(id);
        String mg = new ControlPanelMain().createProject(vo);
        
        JOptionPane.showMessageDialog(parent, mg,"Aviso", JOptionPane.INFORMATION_MESSAGE, 
                        new ImageIcon(getClass().getResource("/presentation/view/images/Ok.png")));
        notifyObserver();
        parent.dispose();
    }//GEN-LAST:event_jButtonSaveProjectActionPerformed

    private void jButtonExitPanelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonExitPanelActionPerformed
        parent.dispose();

    }//GEN-LAST:event_jButtonExitPanelActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonExitPanel;
    private javax.swing.JButton jButtonSaveProject;
    private javax.swing.JComboBox jComboBoxProjectType;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JTextField jTextFieldProjectName;
    // End of variables declaration//GEN-END:variables

    @Override
    public void attach(Observer observer) {
        observers.add(observer);
    }

    @Override
    public void notifyObserver() {
        for (Observer observer : observers) {
            observer.update();
        }
    }

    @Override
    public void remove(Observer observer) {
        observers.remove(observer);
    }
}
